package csu.web.mypetstore;

import csu.web.mypetstore.intercepter.JwtInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@MapperScan("csu.web.mypetstore.mapper")
@SpringBootApplication
public class PetstoreApplication implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    public static void main(String[] args) {
        SpringApplication.run(PetstoreApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 匹配原路径结构
        String projectRoot = new ApplicationHome(getClass()).getDir().getParentFile().getParent();
        String imagePath = "file:" + projectRoot
                + "/src/main/resources/static/images/";

        registry.addResourceHandler("/images/**")
                .addResourceLocations(imagePath);
    }

    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/token", "/account","/images/*"); // 不拦截登录和注册

    }
}
